package yxy.apple.util;

import java.util.UUID;

public class IDGenerator {

	private static final char[] NumberTable = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	private static final char[] HexCharacterTable = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
			'd', 'e', 'f' };

	private static final char[] LetterAndNumberTable = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
			'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
			'x', 'y', 'z' };

	private static final char[] LetterTable = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
			'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

	public static String nextUUID() {
		return UUID.randomUUID().toString();
	}

	// A58057B5-7C47-4765-B569-355D9EE9312B
	// 17B8B532-F5AD-4489-AB14-0C6F18214808
	/**
	 * 生成格式为 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 的OpenUDID
	 * 
	 * @return
	 */
	@Deprecated
	public static String nextOpenUDID() {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < 8; i++) {
			char c = HexCharacterTable[RandomUtility.nextInt(0, HexCharacterTable.length)];
			buffer.append(c);
		}
		buffer.append('-');
		for (int i = 0; i < 4; i++) {
			char c = HexCharacterTable[RandomUtility.nextInt(0, HexCharacterTable.length)];
			buffer.append(c);
		}
		buffer.append('-');
		for (int i = 0; i < 4; i++) {
			char c = HexCharacterTable[RandomUtility.nextInt(0, HexCharacterTable.length)];
			buffer.append(c);
		}
		buffer.append('-');
		for (int i = 0; i < 4; i++) {
			char c = HexCharacterTable[RandomUtility.nextInt(0, HexCharacterTable.length)];
			buffer.append(c);
		}
		buffer.append('-');
		for (int i = 0; i < 12; i++) {
			char c = HexCharacterTable[RandomUtility.nextInt(0, HexCharacterTable.length)];
			buffer.append(c);
		}
		return buffer.toString().toUpperCase();
	}

	// "1D:EF:37:27:BE:98"
	public static String nextMacAddress(boolean uppercase) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 2; j++) {
				char c = HexCharacterTable[RandomUtility.nextInt(0, HexCharacterTable.length)];
				if (uppercase) {
					buffer.append(Character.toUpperCase(c));
				} else {
					buffer.append(c);
				}
			}
			if (i < 5) {
				buffer.append(":");
			}
		}
		return buffer.toString();
	}

	public static String nextNumberSequence(int length) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < length; i++) {
			char c = NumberTable[RandomUtility.nextInt(0, NumberTable.length)];
			buffer.append(c);
		}
		return buffer.toString();
	}

	public static String nextHexSequence(int length, boolean uppercase) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < length; i++) {
			char c = HexCharacterTable[RandomUtility.nextInt(0, HexCharacterTable.length)];
			if (uppercase) {
				buffer.append(Character.toUpperCase(c));
			} else {
				buffer.append(c);
			}
		}
		return buffer.toString();
	}

	public enum LetterCase {
		Lowercase, Uppercase, RandomCase,
	}

	/**
	 * 使用指定table中的元素随机生成长度为length的sequence，字母为小写
	 * 
	 * @param table
	 * @param length
	 * @return
	 */
	public static String nextCharSequence(char[] table, int length) {
		return nextCharSequence(table, length, LetterCase.Lowercase);
	}

	/**
	 * 使用指定table中的元素随机生成长度为length的sequence，字母会按cased要求设定大小写
	 * 
	 * @param table
	 * @param length
	 * @param cased
	 * @return
	 */
	public static String nextCharSequence(char[] table, int length, LetterCase cased) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < length; i++) {
			char c = table[RandomUtility.nextInt(0, table.length)];
			switch (cased) {
			case Uppercase:
				buffer.append(Character.toUpperCase(c));
				break;
			case RandomCase:
				if (RandomUtility.nextBoolean()) {
					buffer.append(Character.toUpperCase(c));
				} else {
					buffer.append(c);
				}
				break;
			case Lowercase:
			default:
				buffer.append(c);
				break;
			}
		}
		return buffer.toString();
	}

	/**
	 * 生成长度为length的字母和数字混排的sequnece，字母为小写
	 * 
	 * @param length
	 * @return
	 */
	public static String nextLetterAndNumberSequence(int length) {
		return nextLetterAndNumberSequence(length, LetterCase.Lowercase);
	}

	/**
	 * 生成长度为length的字母和数字混排的sequnece，按LetterCase确定字母大小写
	 * 
	 * @param length
	 * @param cased
	 * @return
	 */
	public static String nextLetterAndNumberSequence(int length, LetterCase cased) {
		return nextCharSequence(LetterAndNumberTable, length, cased);
	}

	/**
	 * 生成长度为length的字母sequence，小写字母
	 * 
	 * @param length
	 * @return
	 */
	public static String nextLetterSequence(int length) {
		return nextLetterSequence(length, LetterCase.Lowercase);
	}

	/**
	 * 生成长度为length的字母sequence，大小写按cased确定
	 * 
	 * @param length
	 * @param cased
	 * @return
	 */
	public static String nextLetterSequence(int length, LetterCase cased) {
		return nextCharSequence(LetterTable, length, cased);
	}

}
